Skip to main content

Keywords

Banner java icon

In total, Java has 51 reserved words that have very specific meanings and cannot be used as identifiers in the application code.

Also, 16 contextual keywords are treated as keywords when found in a specific context. Programmers should not use these keywords for anything other than what they are meant to be.

Java Keywords & Contextual Keywords

What is a Keyword in Java?

Keywords are reserved words in Java with special meanings. They cannot be used for variables, class names, or methods.

Example:

int age; // 'int' is a keyword, can't be used as a variable name

Fun Fact: If Java let us use keywords as variables, we'd have nightmares debugging code like:

int int = 5; if if = true then do while while = false!

Java Keywords

Here are the 51 keywords you can't use as identifiers:

KeywordDescription
abstractDefines an incomplete class/method.
assertHelps test assumptions.
booleanHolds true or false.
breakExits loops or switch cases.
byteStores numbers from -128 to 127.
caseDefines cases in switch statements.
catchHandles exceptions.
charStores a single character.
classDefines a class.
constReserved but unused. Use final instead.
continueSkips to next loop iteration.
defaultSpecifies default behavior in switch and interfaces.
doExecutes code at least once in loops.
double64-bit floating point.
elseDefines an alternative branch in if-statements.
enumCreates an enumeration.
extendsExtends a class.
finalPrevents modification. for info on final
finallyAlways executes after try-catch. for info on finally
float32-bit floating point.
forStarts a for-loop.
gotoReserved but not used.
ifCreates a condition.
implementsImplements an interface.
importImports packages/classes.
instanceofChecks object type.
intStores 32-bit integers.
interfaceDefines an interface.
longStores 64-bit integers.
nativeIndicates platform-specific code.
newCreates objects.
packageDeclares a package.
privateLimits access to a class.
protectedAllows access within package & subclasses.
publicAllows access everywhere.
returnReturns from a method.
shortStores 16-bit integers.
staticBelongs to the class, not instances. for info on static
strictfpEnsures floating-point consistency.
superRefers to parent class.
switchAllows multiple execution paths.
synchronizedEnsures thread safety.
thisRefers to the current object.
throwThrows an exception.
throwsDeclares possible exceptions.
transientExcludes variable from serialization.
tryDefines error-handling code.
voidSpecifies no return value.
volatilePrevents variable caching.
whileLoops while condition is true.
_ (underscore)Prevents underscore as an identifier (Java 9+).

Example:

class SuperHero {
final String name = "JavaMan"; // Cannot be changed
public void fly() { System.out.println("Up, up and compile!"); }
}

Contextual Keywords

These 16 words are keywords only in certain contexts.

KeywordDescription
exportsUsed in Java modules.
moduleDeclares modules.
non-sealedDefines flexible sealed classes.
openOpens modules.
opensAllows reflection access.
permitsLists subclasses for a sealed class.
providesSpecifies service providers.
recordDefines immutable data types.
requiresDeclares module dependencies.
sealedRestricts class hierarchy.
toDefines dependencies between modules.
transitiveIndicates inherited dependencies.
usesDeclares service dependencies.
varAllows local type inference.
withUsed in module directives.
yieldReturns a value from a switch expression.

Example:

sealed interface Superhero permits Superman, Batman {}
record Batman(String gadget) implements Superhero {}

Fun Fact: Java 15 introduced sealed, making sure your superhero team doesn’t get random recruits like ClownMan!

Now go forth and Java like a pro! 🚀